package yy.yyds.org.controller;

import io.swagger.annotations.Api;
import jdk.nashorn.internal.runtime.FindProperty;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import yy.yyds.basic.query.util.AjaxResult;
import yy.yyds.basic.query.util.QueryResult;
import yy.yyds.org.domain.Department;
import yy.yyds.org.query.DepartmentQuery;
import yy.yyds.org.service.IDepartmentService;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@RestController//ResponseBody+@Controller
@RequestMapping("/dept")
@Api(tags = "部门接口",description = "部门接口详细描述")
public class DepartmentController {

    @Autowired
    private IDepartmentService departmentService;

    //@RequestMapping("/querById")
    /*
    * @PathVariable从url地址中取值
    * 此时请求方式：get请求http://localhost:8080/dept/3
     * */
    @GetMapping("/{id}")

    public Department querById(@PathVariable Long id){
        return departmentService.loadOne(id);
    }
    /*查询所有*/
    @GetMapping
    public List<Department> querAll(){
        return departmentService.loadAll();
    }

    /*分页查询
    * @RequestBody:从请求体中获取json格式的参数
    * @RequestParam：从请求体或者url路径中获取 username=张三&password=123  键值对格式
    */
    @PostMapping
    public QueryResult<Department> queryPage(@RequestBody DepartmentQuery query){
        return  departmentService.querypage(query);
    }

    /*根据id删除一条

    * */
    @DeleteMapping("/{id}")
    public AjaxResult deleteById(@PathVariable long id){
        try {
            departmentService.delete(id);
            return  new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return   AjaxResult.me().setSuccess(false).setMessage("操作成功");
        }
    }
    /*修该改和新增*/
    @PutMapping
    public AjaxResult addOrUpdate(@RequestBody Department department) {
        try {
            if (department.getId() == null) {
                departmentService.add(department);
            } else {
                departmentService.update(department);
            }
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("操作失败");
        }
    }

    /*批量删除*/
    @PatchMapping
    public AjaxResult batchDelete(@RequestBody List<Long> ids){
        departmentService.batchDelete(ids);
        try {
            departmentService.batchDelete(ids);
            return  new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return   AjaxResult.me().setSuccess(false).setMessage("操作成功");
        }
    }
    /*获取部门树*/
    /*嵌套循环  性功不好*/
    @GetMapping("/getTree")
    /*public List<Department> getTree(){
        List<Department> all = departmentService.loadAll();

        ArrayList<Department> result = new ArrayList<>();
        //2.遍历所有 如果是顶级部门 就放在result
        for(Department department : all){
            if(department.getParent_id() == null){//如果是顶级部门 就放在result
                result.add(department);
            }else {//3.如果不是顶级部门 那就找父部门
                for (Department parent : all){
                    if(department.getParent_id() == parent.getId()){//找到啦父部门
                        List<Department> children = parent.getChildren();
                        children.add(department);
                        break; // 找到父部门了  结束内存循环
                    }
                }
            }
        }
            return  result;
    }*/
    /*使用Map方式*/
    public List<Department> getTree(){
        List<Department> all = departmentService.loadAll();
        //把所有部门转成 map key是部门id  valure就是这个部门对象
       Map<Long, Department> map = all.stream().collect(Collectors.toMap(Department::getId, item -> item));

        //声明好要返回的集合 装顶级部门
        ArrayList<Department> result = new ArrayList<>();
        //2.遍历所有 如果是顶级部门 就放在result
        for(Department department : all){
            if(department.getParent_id() == null){//如果是顶级部门 就放在result
                result.add(department);
            }else {//3.如果不是顶级部门 那就找父部门
                Long pid=  department.getParent_id();
                Department parent = map.get(pid);
                if(parent != null) {
                    List<Department> children = parent.getChildren();
                    children.add(department);
                }
            }
        }
        return  result;
    }
}
